import {
  request
} from '../../api/index'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import {
  formatTime
} from '../../utils/util';

// 获取应用实例
const app = getApp()

Page({
  data: {
    refresh: false,
    onLoading: false,
    dataSource: [],
    safeArea: 0,
    page: {
      name: '',
      currentPage: 1,
      size: 7,
      isAsc: false
    },
    total: 50
  },
  onRefresh() {
    this.setData({
      refresh: true,
      page: {
        ...this.data.page,
        currentPage: 1
      }
    })
    this.fetchData();
    setTimeout(() => {
      this.setData({
        refresh: false
      })
    }, 1200)
  },
  onRefreshEnd() {},
  onScrollLoad() {
    if (this.data.total > this.data.dataSource.length) {
      this.setData({
        onLoading: true,
        page: {
          ...this.data.page,
          currentPage: this.data.page.currentPage + 1
        }
      })
      this.loadData();
      setTimeout(() => {
        this.setData({
          onLoading: false
        })
      }, 1200)
    }
  },
  inputHandle(e) {
    this.setData({
      page: {
        ...this.data.page,
        name: e.detail.value
      }
    })
  },
  clearInput() {
    this.setData({
      page: {
        ...this.data.page,
        name: ''
      }
    })
    this.fetchData();
  },
  loadData() {
    request.collectionRecord(this.data.page, (e) => {
      const data = e?.records.map(item => {
        return {
          ...item,
          time: this.formatTime(item.createTime)
        }
      })
      this.setData({
        dataSource: [...this.data.dataSource, ...data],
        total: e.total,
        page: {
          ...this.data.page,
          currentPage: e.current
        }
      })
    }, function (e) {
      Toast({
        message: e,
        position: 'bottom'
      });
    })
  },
  asc() {
    this.setData({
      page: {
        ...this.data.page,
        currentPage: 1,
        isAsc: !this.data.page.isAsc,
      }
    })
    this.fetchData();
  },
  searchHandle(e) {
    this.setData({
      page: {
        ...this.data.page,
        name: e.detail.value,
        currentPage: 1
      }
    })
    this.fetchData();
  },
  /**
   * 生命周期
   */
  onReady() {
  },
  onLoad() {
    this.setPageProperty();
    this.fetchData();
    wx.getSystemInfo().then(res => {
      if (res.errMsg === 'getSystemInfo:ok') {
        this.setData({
          safeArea: res.safeArea.bottom - res.safeArea.height + 'px'
        })
      }
    })
  },
  /**
   * 设置页面属性
   */
  setPageProperty() {
    wx.setNavigationBarTitle({
      title: '陶瓷数据采集列表'
    })
  },
  fetchData() {
    wx.showLoading({
      title: '加载中'
    });
    setTimeout(() => {
      wx.hideLoading();
    }, 600);
    request.collectionRecord(this.data.page, (e) => {
      const data = e?.records.map(item => {
        return {
          ...item,
          time: this.formatTime(item.createTime)
        }
      })
      this.setData({
        dataSource: data,
        total: e.total,
        page: {
          ...this.data.page,
          currentPage: e.current
        }
      })
    }, function (e) {
      Toast({
        message: e,
        position: 'bottom'
      });
    })
  },
  edditHandle(event) {
    wx.redirectTo({
      url: event.currentTarget.dataset.source.status? '../navigation/index?id=' + event.currentTarget.dataset.source.id : '../new-data/index?id=' + event.currentTarget.dataset.source.id
    })
  },
  formatTime(time) {
    const t = time.replace(/-/g, '/');
    const d = new Date(t);
    return formatTime(d);
  },
  onAdd() {
    wx.navigateTo({
      url: '/pages/add-record/index',
    })
  },

  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    wx.onAppRoute((res) => {
      if (res.path === 'pages/index/index' && res.openType === 'navigateBack') {
        this.setPageProperty();
        this.setData({
          page: {
            ...this.data.page,
            currentPage: 1
          }
        })
        this.fetchData();
      }
    })
  },
  /** 
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '陶瓷数据采集',
      path: 'pages/index/index',
      imageUrl: 'https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2021%2F0623%2Fe5aa631fj00qv43qy004nc000qo00hzm.jpg&thumbnail=660x2147483647&quality=80&type=jpg'
    }
  },
})